#!/bin/bash
proc() {
    echo -e "\033[42;32m > $@ \033[0m" >&2
    local command=("$@")
    if "${command[@]}"; then
        echo -e "\033[42;32m SUCCEED \033[0m \n" >&2
    else
        echo -e "\033[41;32m FAILED \033[0m \n" >&2
        exit 1
    fi
}
v3spath=/home/raii/KzLab/v3s
kernelpath=${v3spath}/linux-zero-5.2.y
ubootpath=${v3spath}/u-boot
mkimgpath=${v3spath}/mkimg

# 编译
proc runuser -l raii -c '/home/raii/KzLab/v3s/mkimg/buildKernel.sh'

# 挂载SD卡
proc ./mount.sh $1

# 拷贝输出件至当前目录
proc cp ${ubootpath}/u-boot-sunxi-with-spl.bin .
proc cp ${kernelpath}/arch/arm/boot/zImage .
proc cp ${kernelpath}/arch/arm/boot/dts/sun8i-v3s-licheepi-zero.dtb .

# 将输出件写入存储
proc dd if=u-boot-sunxi-with-spl.bin of=/dev/$1 bs=1024 seek=8 # 烧录uboot
proc cp zImage sun8i-v3s-licheepi-zero.dtb ${mkimgpath}/fs1 # 拷贝kernel和dtb
proc cd ${kernelpath}
proc make -j16 modules_install INSTALL_MOD_PATH=${mkimgpath}/fs2
sleep 1

# 写入Wifi驱动ko和bin
# proc cp ${kernelpath}/drivers/net/wireless/realtek/rtlwifi/rtl8188eu/8188eu.ko .
# proc cp ${kernelpath}/drivers/net/wireless/realtek/rtlwifi/rtl8188eu/rtl8188eufw.bin .
# proc cp ./8188eu.ko ${mkimgpath}/fs2/lib/modules
# proc cp ./rtl8188eufw.bin ${mkimgpath}/fs2/lib/firmware/rtlwifi


# 卸载SD卡
proc cd ${mkimgpath}
proc ./umount.sh $1